import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StatePatternModule {
    private final Scanner scanner = new Scanner(System.in);
    private final List<MachineState> machineStates = new ArrayList<>();
    private final FactoryPatternModule factoryModule;
    
    public StatePatternModule(FactoryPatternModule factoryModule) {
        this.factoryModule = factoryModule;
    }
    
    public void execute() {
        // 初始化状态
        if (machineStates.isEmpty()) {
            for (int i = 0; i < factoryModule.getMachines().size(); i++) {
                machineStates.add(new IdleState());
            }
        }
        
        while (true) {
            System.out.println("\n===== 农机状态管理 =====");
            System.out.println("1. 查看农机状态");
            System.out.println("2. 更改农机状态");
            System.out.println("3. 返回主菜单");
            System.out.print("请选择操作(1-3): ");
            
            int choice = scanner.nextInt();
            scanner.nextLine();  // 消耗换行符
            
            switch (choice) {
                case 1 -> viewMachineStates();
                case 2 -> changeMachineState();
                case 3 -> { return; }
                default -> System.out.println("无效选择，请重新输入");
            }
        }
    }
    
    private void viewMachineStates() {
        List<AgriculturalMachine> machines = factoryModule.getMachines();
        if (machines.isEmpty()) {
            System.out.println("没有可用的农机设备");
            return;
        }
        
        System.out.println("\n=== 农机状态 ===");
        for (int i = 0; i < machines.size(); i++) {
            System.out.println((i+1) + ". " + machines.get(i).getStatus() + 
                              " | 状态: " + machineStates.get(i).getStateName());
        }
    }
    
    private void changeMachineState() {
        viewMachineStates();
        List<AgriculturalMachine> machines = factoryModule.getMachines();
        if (machines.isEmpty()) return;
        
        System.out.print("选择要更改状态的农机编号: ");
        int index = scanner.nextInt() - 1;
        scanner.nextLine();  // 消耗换行符
        
        if (index < 0 || index >= machines.size()) {
            System.out.println("无效的农机编号");
            return;
        }
        
        System.out.println("1. 空闲状态");
        System.out.println("2. 工作状态");
        System.out.println("3. 故障状态");
        System.out.print("选择新状态(1-3): ");
        int stateChoice = scanner.nextInt();
        scanner.nextLine();  // 消耗换行符
        
        switch (stateChoice) {
            case 1 -> machineStates.set(index, new IdleState());
            case 2 -> machineStates.set(index, new WorkingState());
            case 3 -> machineStates.set(index, new FaultState());
            default -> System.out.println("无效选择");
        }
        
        machineStates.get(index).handleState();
    }
    
    public MachineState getMachineState(int index) {
        if (index >= 0 && index < machineStates.size()) {
            return machineStates.get(index);
        }
        return null;
    }
}